package klab;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

//import javax.swing.ImageIcon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

import sun.awt.HorizBagLayout;
import sun.awt.VerticalBagLayout;

/**
 * Panel sa check boxovima koji reprezentuju racunare
 */
public class Racunari extends JPanel{
	private static final long serialVersionUID = 5386082081988428149L;
	SysOperacije sysOp = SysOperacije.getInstance();	
	JPanel redovi   = new JPanel();
	JPanel kontrola = new JPanel();
	JPanel cosak    = new JPanel();
	JPanel desni    = new JPanel();
	
	JRadioButton svi   = new JRadioButton("svi");
	JButton invert = new JButton("invertuj");

	// Generise CheckBox-ove koji predstavljaju racunare i smesta ih u mapu racunari
	public Racunari(){
		super();
		init();
	}
	/**
	 * Sredjivanje izgleda elementa Racunari
	 */
	private void init(){
		Iterator<JCheckBox> it = sysOp.racunari.iterator();
		JPanel panel;
		ArrayList<JPanel> paneli = new ArrayList<JPanel>(); // Za obrtanje redosleda
		redovi.setLayout(new VerticalBagLayout());
		for ( int j = 1; j <= sysOp.BR_REDOVA;j++ ){
			panel = new JPanel();
			panel.setLayout(new HorizBagLayout());
			for ( int i=1; i <= sysOp.RACUNARA_U_REDU ;i++ )
				if (it.hasNext())
					panel.add(it.next());
			panel.setPreferredSize(new Dimension(sysOp.RACUNARA_U_REDU*55,25));
			paneli.add(panel);
		}
		kontrola.setPreferredSize(paneli.get(0).getPreferredSize());
		kontrola.setLayout(new HorizBagLayout());
		redovi.add(kontrola);
		// Obrni redosled redova
		for (int i = sysOp.BR_REDOVA-1; i >= 0 ; i--){
			redovi.add(paneli.get(i));
		}
		
		svi.setPreferredSize(new Dimension(45,25));

		// crtezi ikonica za prikaz RadioButton-a svi
		ClassLoader cldr = this.getClass().getClassLoader();
		URL imageURL   = cldr.getResource("unchecked.png");
		svi.setIcon(new ImageIcon(imageURL));
		imageURL   = cldr.getResource("checked.png");
		svi.setSelectedIcon(new ImageIcon(imageURL));
		imageURL   = cldr.getResource("uncheckedrollover.png");
		svi.setRolloverIcon(new ImageIcon(imageURL));
		imageURL   = cldr.getResource("checkedrollover.png");
		svi.setRolloverSelectedIcon(new ImageIcon(imageURL));
		
		svi.setBackground(new Color(230,230,160));
		svi.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				for (JCheckBox cb : sysOp.racunari){
					cb.setSelected(svi.isSelected());
				}	
			}
		});
		invert.setPreferredSize(new Dimension(77,20));
		invert.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				for (JCheckBox cb : sysOp.racunari){
					cb.setSelected(!cb.isSelected());
				}
			}
		});
		invert.setBackground(new Color(230,230,160));
		kontrola.add(svi);
		kontrola.add(invert);
		
		JPanel prazno = new JPanel(); // prazan prostor
		prazno.setPreferredSize(new Dimension(100,25));

		int preostalo = sysOp.racunari.size() - sysOp.BR_REDOVA * sysOp.RACUNARA_U_REDU;
		cosak.setPreferredSize(new Dimension(110,(((int)preostalo/2)+(preostalo%2))*paneli.get(0).getPreferredSize().height));
		
		cosak.setLayout(new GridLayout((preostalo/2)+(preostalo%2),2));
		for (int i=sysOp.BR_REDOVA * sysOp.RACUNARA_U_REDU+1; i <= sysOp.racunari.size(); i++)
			cosak.add(sysOp.racunari.get(i-1));
		
		desni.setPreferredSize(new Dimension(110,100));
		desni.setLayout(new VerticalBagLayout());
		
		desni.add(prazno);
		desni.add(cosak);
		
		setLayout(new BorderLayout());
		setBorder(new TitledBorder(" računari (klijenti) "));
		add (redovi,BorderLayout.WEST);
		if (preostalo>0)
			add (desni,BorderLayout.EAST);
	}
}